package cn.edu.hunau.service.impl;

import cn.edu.hunau.dao.BackstageDao;
import cn.edu.hunau.dao.impl.BackstageDaoImpl;
import cn.edu.hunau.exception.DaoException;
import cn.edu.hunau.exception.UsernameOrPwdErrorException;
import cn.edu.hunau.pojo.User;
import cn.edu.hunau.service.BackstageService;
import cn.edu.hunau.utils.OrderResultUtils;

import java.util.*;

/**
 * @Author 苏佳
 * @Date 2023 01 04 09 07
 **/
public class BackstageServiceImpl implements BackstageService {
    private BackstageDao backstageDao = new BackstageDaoImpl();

    @Override
    public Map<String,Map<String, Integer>> countingOrder() throws DaoException {
        HashMap<String, Map<String, Integer>> maps = new HashMap<>();
        //各月份订单情况
        Map<String, Integer> map1 = OrderResultUtils.orderResultByMonth(1);
        Map<String, Integer> map2 = OrderResultUtils.orderResultByMonth(2);
        Map<String, Integer> map3 = OrderResultUtils.orderResultByMonth(3);
        Map<String, Integer> map4 = OrderResultUtils.orderResultByMonth(4);
        Map<String, Integer> map5 = OrderResultUtils.orderResultByMonth(5);
        Map<String, Integer> map6 = OrderResultUtils.orderResultByMonth(6);
        Map<String, Integer> map7 = OrderResultUtils.orderResultByMonth(7);
        Map<String, Integer> map8 = OrderResultUtils.orderResultByMonth(8);
        Map<String, Integer> map9 = OrderResultUtils.orderResultByMonth(9);
        Map<String, Integer> map10 = OrderResultUtils.orderResultByMonth(10);
        Map<String, Integer> map11 = OrderResultUtils.orderResultByMonth(11);
        Map<String, Integer> map12 = OrderResultUtils.orderResultByMonth(12);
        maps.put("Jan",map1);
        maps.put("Feb",map2);
        maps.put("Mar",map3);
        maps.put("Apr",map4);
        maps.put("May",map5);
        maps.put("Jun",map6);
        maps.put("Jul",map7);
        maps.put("Aug",map8);
        maps.put("Sep",map9);
        maps.put("Oct",map10);
        maps.put("Nov",map11);
        maps.put("Dec",map12);
        return maps;
    }

    @Override
    public Map<String, Integer> countingRegisterNum() throws DaoException {
        Map<String, Integer> map = new HashMap<>();
        //查询1-12月的注册人数
        int Jan= backstageDao.countByMonth(1);
        map.put("Jan",Jan);
        int Feb= backstageDao.countByMonth(2);
        map.put("Feb",Feb);
        int Mar= backstageDao.countByMonth(3);
        map.put("Mar",Mar);
        int Apr= backstageDao.countByMonth(4);
        map.put("Apr",Apr);
        int May= backstageDao.countByMonth(5);
        map.put("May",May);
        int Jun= backstageDao.countByMonth(6);
        map.put("Jun",Jun);
        int Jul= backstageDao.countByMonth(7);
        map.put("Jul",Jul);
        int Aug= backstageDao.countByMonth(8);
        map.put("Aug",Aug);
        int Sep= backstageDao.countByMonth(9);
        map.put("Sep",Sep);
        int Oct= backstageDao.countByMonth(10);
        map.put("Oct",Oct);
        int Nov= backstageDao.countByMonth(11);
        map.put("Nov",Nov);
        int Dec= backstageDao.countByMonth(12);
        map.put("Dec",Dec);

        return map;
    }

    @Override
    public Map<String, Integer> topSellingSum() throws DaoException {
        Map<String, Integer> map = new HashMap<>();
        int sum1 = backstageDao.queryCategorySumByCid(1);
        int sum2 = backstageDao.queryCategorySumByCid(2);
        int sum3 = backstageDao.queryCategorySumByCid(3);
        map.put("cid1",sum1);
        map.put("cid2",sum2);
        map.put("cid3",sum3);
        return map;
    }

    @Override
    public User login(String username, String pwd) throws DaoException {
        //逻辑的实现 : 对数据格式进行判断
        String regex="^\\s*$";
        if(username.matches(regex) || pwd.matches(regex)){
            throw new RuntimeException("参数格式异常!");
        }

        User user = backstageDao.queryByUserNameAndPwd(username, pwd);
        if(Objects.isNull(user)){
            //用户名或者密码错误
            throw new UsernameOrPwdErrorException("用户名或者密码错误!");
        }
        return user;
    }
}
